package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/7/18 11:40
 * 功能：
 */
public class ProfileSettingsActivity extends BaseActivity {

    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.relative1)
    RelativeLayout relative1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.relative2)
    RelativeLayout relative2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.relative3)
    RelativeLayout relative3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.relative4)
    RelativeLayout relative4;

    private String value;
    private int state;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_profile_settings;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        getConfig();
    }


    public void alert_label(String text, int state) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        LayoutInflater factory = LayoutInflater.from(this);//提示框
        final View view = factory.inflate(R.layout.dialog_edit, null);//这里必须是final的
        final EditText et = view.findViewById(R.id.editText);
        et.setHint(text);
        builder.setTitle(text);

        final AlertDialog dialog = builder.create();
        dialog.setView(view);

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(et.getText().toString())) {

                    value = et.getText().toString();
                    String name = "";

                    switch (state) {

                        case 1:
                            name = "imgtextfee";
                            break;
                        case 2:
                            name = "asknum";
                            break;
                        case 3:
                            name = "videofee";
                            break;
                        case 4:
                            name = "callfee";
                            break;

                    }

                    config(name, value);
                    dialog.dismiss();

                } else {

                    Toast("非法输入!");

                }



            }
        });



    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    Log.e("问诊参数","参数"+msg.obj.toString());

                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        JSONObject object = jsonObject.getJSONObject("data");
                        text1.setText(object.getString("imgtextfee")+"元");
                        text2.setText(object.getString("asknum")+"次");
                        text3.setText(object.getString("videofee")+"元");
                        text4.setText(object.getString("callfee")+"元");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;
                case 2:

                    Log.e("问诊参数","参数"+msg.obj.toString());

                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        Toast(jsonObject.getString("msg"));

                        switch (state) {

                            case 1:
                                text1.setText(value+"元");
                                break;
                            case 2:
                                text2.setText(value+"次");
                                break;
                            case 3:
                                text3.setText(value+"元");
                                break;
                            case 4:
                                text4.setText(value+"元");
                                break;

                        }


                    }catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    public void getConfig() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getConfig");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);

    }

    public void config(String name, String value) {

        List<String> l = new ArrayList<>();
        l.add(value);

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "setConfig");
        params.put("data[" + name + "]", value);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 2, 404);

    }


    @OnClick({R.id.relative1, R.id.relative2, R.id.relative3, R.id.relative4})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.relative1:

                state = 1;
                alert_label("图文问诊价格", state);

                break;
            case R.id.relative2:

                state = 2;
                alert_label("图文问诊次数", state);

                break;
            case R.id.relative3:

                state = 3;
                alert_label("视频问诊价格", state);

                break;
            case R.id.relative4:

                state = 4;
                alert_label("电话问诊价格", state);

                break;
        }
    }
}
